package com.qiantu.phone.ui.activity;

import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hjq.http.listener.HttpCallback;
import com.qiantu.api.db.LLControllerDBManager;
import com.qiantu.api.db.LLDeviceDBManager;
import com.qiantu.api.db.LLDeviceGroupDBManager;
import com.qiantu.api.entity.ControllerBean;
import com.qiantu.api.entity.DeviceBean;
import com.qiantu.api.entity.DeviceBindGroupBean;
import com.qiantu.api.http.LLHttpManager;
import com.qiantu.api.http.model.HttpData;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.event.RefreshDeviceListEvent;
import com.qiantu.phone.ui.adapter.DeviceGroupListAdapter;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;

public class  GroupEnrollmentActivity extends AppActivity {
    private RecyclerView list;
    private DeviceGroupListAdapter adapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_device_group_list;
    }

    @Override
    protected void initView() {
        setOnClickListener(R.id.btn_addOtherGroup);
        list = findViewById(R.id.list);
        list.setLayoutManager(new LinearLayoutManager(getContext()));
        adapter = new DeviceGroupListAdapter(getContext(), true, true);
        list.setAdapter(adapter);
    }

    @Override
    protected void initData() {
        String controllerSerialNo = getIntent().getStringExtra("controllerSerialNo");
        List<String> containDeviceBeans = getIntent().getStringArrayListExtra("containDevice");
        ControllerBean controllerBean = LLControllerDBManager.getInstance(getContext()).getControllerBySerialNo(controllerSerialNo);
        List<DeviceBean> deviceBeans = LLDeviceDBManager.getInstance(getContext()).getDeviceByControllerSerialNoOnlyGroupDevice(controllerSerialNo);
        for(int i = 0;i < containDeviceBeans.size();i++){
            String containDeviceSerialNo = containDeviceBeans.get(i);
            for(int j = 0;j < deviceBeans.size();j++){
                DeviceBean deviceBean = deviceBeans.get(j);
                if(containDeviceSerialNo.equals(deviceBean.getDeviceSerialNo())){
                    deviceBeans.remove(j);
                    break;
                }
            }
        }
        controllerBean.setDeviceItems(deviceBeans);
        List<ControllerBean> controllerBeans = new ArrayList<>();
        controllerBeans.add(controllerBean);
        adapter.setData(controllerBeans);
    }



    @Override
    public void onRightClick(View view) {
        if(adapter.getSelectedData() == null || adapter.getSelectedData().size() == 0){
            toast(R.string.please_select_room_hint);
            return;
        }
        String deviceSerialNo = getIntent().getStringExtra("deviceSerialNo");
        DeviceBean groupDeviceBean = adapter.getSelectedData().get(0);
        LLHttpManager.groupJoinDevice(this, groupDeviceBean.getDeviceSerialNo(),deviceSerialNo, new HttpCallback<HttpData<Void>>(this) {
            @Override
            public void onStart(Call call) {
                showDialog();
            }

            @Override
            public void onSucceed(HttpData<Void> result) {
                toast(R.string.reset_success);
                DeviceBindGroupBean bindGroupBean = new DeviceBindGroupBean();
                bindGroupBean.setDeviceBindGroupSerialNo("");
                bindGroupBean.setDeviceSerialNo(deviceSerialNo);
                bindGroupBean.setDeviceName("");
                bindGroupBean.setDeviceType(groupDeviceBean.getDeviceType());
                bindGroupBean.setDeviceGroupSerialNo(groupDeviceBean.getDeviceSerialNo());
                bindGroupBean.setDeviceGroupName(groupDeviceBean.getName());

                LLDeviceGroupDBManager.getInstance(getContext()).addSingleGroupData(bindGroupBean);
                EventBus.getDefault().post(new RefreshDeviceListEvent());
                finish();
            }

            @Override
            public void onFail(Exception e) {
                super.onFail(e);
            }

            @Override
            public void onEnd(Call call) {

            }
        });
    }

    @Override
    protected  void onDestroy(){
        super.onDestroy();
    }
}
